﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using Sgml;
using System.Xml;
using Softyum.Common.net;

namespace Softyum.ForexAgent.Forms
{
    public partial class XPathTestForm : Form
    {
        public XPathTestForm()
        {
            InitializeComponent();
        }

        private void XPathTestForm_Load(object sender, EventArgs e)
        {

        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            btnTest.Enabled = false;
            tbResult.ResetText();
            tbHeader.ResetText();

            worker.RunWorkerAsync();
        }

        private void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            WebRequest request = WebRequest.Create(tbUrl.Text);
            WebResponse response = request.GetResponse();

            StringBuilder sb = new StringBuilder();
            foreach (string key in response.Headers.AllKeys)
            {
                sb.AppendFormat("{0}: {1}\r\n", key, response.Headers[key]);
            }
            UpdateHeaderText(sb.ToString());

            Encoding encoding = response.TryGetCharset(Encoding.Default);

            using (StreamReader sr = new StreamReader(response.GetResponseStream(), encoding))
            {
                this.UpdateResultText(ParseBlocks(sr.ReadToEnd()));
            }

        }

        private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            btnTest.Enabled = true;

            if (e.Error != null)
            {
            }
            else if (e.Result != null)
            {
            }
        }

        private string ParseBlocks(string text)
        {
            using (SgmlReader reader = new SgmlReader())
            {
                reader.DocType = "HTML";
                reader.InputStream = new StringReader(text);
                reader.WhitespaceHandling = WhitespaceHandling.None;
                XmlDocument doc = new XmlDocument();
                doc.Load(reader);
                XmlNodeList items = doc.SelectNodes(tbXPath.Text);

                StringBuilder sb = new StringBuilder();
                foreach (XmlNode item in items)
                {
                    sb.AppendLine(item.InnerXml);
                    if (cbSingleBlock.Checked)
                    {
                        break;
                    }
                }

                return sb.ToString();
            }
        }

        private void UpdateHeaderText(string text)
        {
            if (tbHeader.InvokeRequired)
            {
                tbHeader.BeginInvoke(new MethodInvoker(() => { tbHeader.AppendText(text); }));
            }
            else
            {
                tbHeader.AppendText(text);
            }
        }

        private void UpdateResultText(string text)
        {
            if (tbResult.InvokeRequired)
            {
                tbResult.BeginInvoke(new MethodInvoker(() => { tbResult.AppendText(text); }));
            }
            else
            {
                tbResult.AppendText(text);
            }
        }

    }
}
